<?php


namespace app\admin\model;
use my\Security;
use think\Db;
use think\Model;

class DriverAppealRecord extends Model
{
    protected $table = "xm_driver_appeal_record";

    /**
     * 申诉表
     */
    public static function appealList($page, $limit, $data)
    {
        $where          = self::filtrate($data);//数据筛选
        $start          = ($page - 1) * $limit;
        $field          = 'a.*,b.driver_phone,c.order_sn';
        $list           = Db::name('driver_appeal_record a')->join('driver_base b','a.driver_id = b.driver_id')->join('order_common c','a.common_id = c.common_id')->where($where)->field($field)->limit($start, $limit)->order('appeal_id desc')->select();
        $count          = Db::name('driver_appeal_record a')->join('driver_base b','a.driver_id = b.driver_id')->join('order_common c','a.common_id = c.common_id')->where($where)->field($field)->count();
        foreach ($list as $key=>$value){
            //查询省份城市
            $province   = Db::name('region')->where(['region_id'=>$value['region_id']])->value('parent_id');
            $provinName = Db::name('region')->where(['region_id'=>$province])->value('region_name');
            $cityName   = Db::name('region')->where(['region_id'=>$value['region_id']])->value('region_name');
            $list[$key]['dressName']    = $provinName.' / '.$cityName;
            $list[$key]['appeal_num']   = appealNum($value['appeal_num']);
            $list[$key]['appeal_time']  = !empty($value['appeal_time'])?date('Y-m-d H:i:s', $value['appeal_time']):'';
            $list[$key]['appeal_price'] = sprintf('%.2f', $value['appeal_price'] / 100);
            $list[$key]['appeal_status']= appealStatus($value['appeal_status']);
            $list[$key]['driver_phone'] = Security::decrypt($value['driver_phone']);
            $list[$key]['dispose_name'] = getAdminInfo($value['dispose_id'],1);
            $list[$key]['dispose_time'] = !empty($value['dispose_time'])?date('Y-m-d H:i:s', $value['dispose_time']):'';
        }
        //$result = [];
        /*foreach ($results as $k => $v) {
            $key=$v['common_id'];
            $province = Db::table('xm_region')->where('region_id', '=', $v['region_id'])->value('parent_id');

            $result[$key]['dressName'] = $provinceName . "/" . $cityName;
            $result[$key]['order_sn'] = Db::table('xm_order_common')->where('common_id', $v['common_id'])->value('order_sn');
            $result[$key]['appeal_id'] = $v['appeal_id'];
            $result[$key]['appeal_num'] = appealNum($v['appeal_num']);
            $result[$key]['appeal_type'] = $v['appeal_type'];
            $result[$key]['appeal_time'] =date('Y/m/d H:i:s', $v['appeal_time']);
            $result[$key]['appeal_price'] = sprintf('%.2f', $v['appeal_price'] / 100);
            $result[$key]['appeal_status'] = appealStatus($v['appeal_status']);
            $result[$key]['driver_phone'] = Security::decrypt($v['driver_phone']);
            $result[$key]['driver_name'] = $v['driver_name'];
            $result[$key]['dispose_name'] = DB::table('xm_admin')->where('admin_id', $v['dispose_id'])->value('user_name');
            if (is_null($v['dispose_time'])){
                $result[$key]['dispose_time'] ="";
            }else{
                $result[$key]['dispose_time'] = date('Y/m/d H:i:s', $v['dispose_time']);
            }
        }
        array_multisort(array_column($result,'appeal_time'),SORT_DESC,$result);*/
        return array(
            'code'  => 0,
            'msg'   => '',
            'count' => $count,
            'data'  => $list
        );
    }

    /**
     * 筛选方法
     * @param $data 数据
     * @return mixed 筛选条件
     */
    private static function filtrate($data)
    {
        //筛选地区
        if (!empty($data['province_id']) && empty($data['city_id'])) {
            $cityArr = Db::name('region')->where('parent_id',$data['province_id'])->column('region_id');
            $where['a.region_id']     = ['in',implode(',',$cityArr)];
        }elseif(!empty($data['province_id']) && !empty($data['city_id'])){
            $where['a.region_id']     = $data['city_id'];
        }
        //筛选司机手机号
        if (!empty($data['driver_phone'])) {
            $where['b.driver_phone'] = Security::encrypt($data['driver_phone']);
        }
        //订单筛选
        if (!empty($data['order_sn'])) {
            $where['c.order_sn'] = $data['order_sn'];
        }
        //筛选处理结果
        if($data['appeal_status'] != ''){
            $where['a.appeal_status'] = $data['appeal_status'];
        }
        //规定时间区间，筛选条件
        if (!empty($data['start_time']) && !empty($data['end_time'])) {
            $where['a.appeal_time']   = ['between', [strtotime($data['start_time']), strtotime($data['end_time'])]];
        }elseif (!empty($data['start_time']) && empty($data['end_time'])){
            $where['a.appeal_time']   = ['>=', strtotime($data['start_time'])];
        }elseif(empty($data['start_time']) && !empty($data['end_time'])){
            $where['a.appeal_time']   = ['<=', strtotime($data['end_time'])];
        }
        return $where;
    }

    //申诉详情页
    public static function lookDetail($appeal_id)
    {
        //查询申诉信息
        $appealInfo                 = Db::name('driver_appeal_record')->where(['appeal_id'=>$appeal_id])->find();
        $appealInfo['driver_phone'] = Security::decrypt(Db::name('driver_base')->where(['driver_id'=>$appealInfo['driver_id']])->value('driver_phone'));
        $province                   = Db::name('region')->where(['region_id'=>$appealInfo['region_id']])->value('parent_id');
        $provinName                 = Db::name('region')->where(['region_id'=>$province])->value('region_name');
        $cityName                   = Db::name('region')->where(['region_id'=>$appealInfo['region_id']])->value('region_name');
        $appealInfo['dressName']    = $provinName.' / '.$cityName;
        $appealInfo['appeal_price'] = sprintf('%.2f', $appealInfo['appeal_price'] / 100);
        $appealInfo['dispose_name'] = getAdminInfo($appealInfo['dispose_id'],1);
        $appealInfo['dispose_time'] = !empty($appealInfo['dispose_time'])?date('Y-m-d H:i:s', $appealInfo['dispose_time']):'';
        $appealInfo['penalty']      = sprintf('%.2f', $appealInfo['penalty'] / 100);
        $appealInfo['unfreeze_price']= sprintf('%.2f', $appealInfo['unfreeze_price'] / 100);
        $appealInfo['fine_time']    = !empty($appealInfo['fine_time'])?date('Y-m-d H:i:s', $appealInfo['fine_time']):'';
        //申诉图片处理
        if(!empty($appealInfo['appeal_proof'])){
            $appealInfo['appeal_proof']= explode(',',$appealInfo['appeal_proof']);
        }
        if(!empty($appealInfo['common_id'])){
            //查询异常订单信息
            $field = 'common_id,order_sn,need_price,on_car_time,start_name,arrive_name,real_distance,abnormal_reason';
            $appealInfo['orderInfo']= Db::name('order_common')->where(['common_id'=>$appealInfo['common_id']])->field($field)->find();
            $appealInfo['orderInfo']['start_name'] = Security::decrypt($appealInfo['orderInfo']['start_name']);
            $appealInfo['orderInfo']['arrive_name']= Security::decrypt($appealInfo['orderInfo']['arrive_name']);
        }else{
            $appealInfo['orderInfo'] = [];
        }
        //查询申诉超过多次记录
        if($appealInfo['appeal_num'] > 1){
            $appealList = Db::name('driver_appeal_record')->where(['appeal_id'=>['<',$appeal_id],'common_id'=>$appealInfo['common_id']])->select();
            foreach($appealList as $key=>$value){
                if(!empty($value['appeal_proof'])){
                    $appealList[$key]['appeal_proof']= explode(',',$value['appeal_proof']);
                }
            }
            $appealInfo['appealList']= $appealList;
        }else{
            $appealInfo['appealList']= [];
        }
        /*
        $data['order'] = Db::table('xm_order_common')->where('order_sn', $order_sn)->find();
        $data['order']['order_type']=1?"网约车":"出租车";
        $data['order']['start_name']=Security::decrypt($data['order']['start_name']);
        $data['order']['arrive_name']=Security::decrypt($data['order']['arrive_name']);
        $data['appeal']=self::where('common_id',$data['order']['common_id'])->order('appeal_id desc')->select();
        if (is_null($data['appeal'][0]['dispose_time'])){
            $data['appeal'][0]['dispose_time']="";
        }
        if (is_null($data['appeal'][1]['dispose_time'])){
            $data['appeal'][1]['dispose_time']="";
        }
        $data['appeal'][0]['dispose_name'] = DB::table('xm_admin')->where('admin_id', $data['appeal'][0]['dispose_id'])->value('user_name');
        if (!is_null($data['appeal'][1]['dispose_time'])){
            $data['appeal'][1]['dispose_name'] = DB::table('xm_admin')->where('admin_id', $data['appeal'][1]['dispose_id'])->value('user_name');
        }
        $data['appeal'][0]['appeal_proof']=explode(',',$data['appeal'][0]['appeal_proof']);
        $data['appeal'][0]['driver_phone']=Security::decrypt($data['appeal'][0]['driver_phone']);
        $data['money']=Db::table('xm_driver_wallet_record')->where('record_type','5')->where('record_id',$data['appeal'][0]['record_id'])->value('money')/100;
        $province = Db::table('xm_region')->where('region_id', '=', $data['appeal'][0]['region_id'])->value('parent_id');
        $cityName = Db::table('xm_region')->where('region_id', '=', $data['appeal'][0]['region_id'])->value('region_name');
        $provinceName = Db::table('xm_region')->where('region_id', '=', $province)->value('region_name');
        $data['region'] = $provinceName . "/" . $cityName;*/
        return $appealInfo;
    }
}
